// pages/base/base.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    keyboardNum: [
      ['AC', 'X', 'F', 'E'],
      ['7', '8', '9', 'D'],
      ['4', '5', '6', 'C'],
      ['1', '2', '3', 'B'],
      [' ', '0', '.', 'A']
    ],
    num2: '0',
    num2List: ['1', '0'],
    num10: '0',
    num10List: ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'],
    num16: '0',
    num16List: ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'],
    inputnum: "num10"
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },
  /**  改变输入进制类型*/
  onChangeInputType: function (e) {
    this.setData({
      inputnum: e.currentTarget.dataset.inputnum
    })
  },
  onAc: function (e) {
    this.setData({
      num2: '0',
      num10: '0',
      num16: '0'
    })
  },
  onDelete: function (e) {
    let numType = this.data.inputnum
    if (numType === 'num2') {
      let thisNum = this.data.num2
      let updateNum = thisNum.substring(0, thisNum.length - 1) === '' ? '0' : thisNum.substring(0, thisNum.length - 1)
      let num10 = parseInt(updateNum, 2)
      let num16 = parseInt(updateNum, 2).toString(16)
      if (thisNum !== '0') {
        this.setData({
          num2: updateNum,
          num10: num10,
          num16: num16
        })
      }

    } else if (numType == 'num10') {
      let thisNum = this.data.num10
      let updateNum = thisNum.substring(0, thisNum.length - 1) === '' ? '0' : thisNum.substring(0, thisNum.length - 1)
      let num2 = parseInt(updateNum).toString(2)
      let num16 =  parseInt(updateNum).toString(16)
      if (thisNum !== '0') {
        this.setData({
          num2: num2,
          num10: updateNum,
          num16: num16
        })
      }
    } else if (numType == 'num16') {
      let thisNum = this.data.num16
      let updateNum = thisNum.substring(0, thisNum.length - 1) === '' ? '0' : thisNum.substring(0, thisNum.length - 1)
      let num2 = parseInt(updateNum, 16).toString(2)
      let num10 = parseInt(updateNum, 16)
      if (thisNum !== '0') {
        this.setData({
          num2: num2,
          num10: num10,
          num16: updateNum
        })
      }
    }
  },
  /** 输入数字 */
  onInputNum: function (e) {
    let num = e.target.dataset.num
    let numType = this.data.inputnum
    if (numType === 'num2') {
      let thisNum = this.data.num2
      let updateNum = thisNum === '0' ? num : thisNum + num
      let num10 = parseInt(updateNum, 2)
      let num16 = parseInt(updateNum, 2).toString(16)
      this.setData({
        num2: updateNum,
        num10: num10,
        num16: num16
      })
    } else if (numType == 'num10') {
      let thisNum = this.data.num10
      let updateNum = thisNum === '0' ? num : thisNum + num
      let num2 = parseInt(updateNum).toString(2)
      let num16 = parseInt(updateNum).toString(16)
      this.setData({
        num2: num2,
        num10: updateNum,
        num16: num16
      })
    }else if (numType == 'num16') {
      let thisNum = this.data.num16
      let updateNum = thisNum === '0' ? num : thisNum + num
      let num2 = parseInt(updateNum,16).toString(2)
      let num10 = parseInt(updateNum ,16)
      this.setData({
        num2: num2,
        num10: num10,
        num16: updateNum
      })
    }

  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})